package com.yjlove.myplay.ui.activity;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2016/12/1.
 */
public class PullToRefreshTestActivity extends BaseActivity {

    private PullToRefreshListView mPullToListView;
    private ArrayAdapter<String> mAdapter;
    private List<String> mDataList;

    private int size = 0;// 满足10条 才会有上拉和下拉,如果不满足则只有下拉

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mAdapter.notifyDataSetChanged();
            mPullToListView.onRefreshComplete();
//            if (size == 10) {
//                // 这里的功能显示不出来没做这个数据，不过功能可以实现
//                mPullToListView.setMode(PullToRefreshBase.Mode.BOTH);
//                //setPullToRefreshLable();
//            } else {
//                mPullToListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//            }
        }
    };

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_ptrtest;
    }

    /**
     * 初始化界面
     */
    protected void initView() {
        mPullToListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        setPullToRefreshLable();
    }

    /**
     * 初始化数据
     */
    protected void initData() {
        mDataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mDataList.add("AiSheBo" + i + "\t号");
        }
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDataList);
        mPullToListView.setAdapter(mAdapter);

        mPullToListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 设置时间格式
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                pullDownToRefresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 设置时间格式
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                pullUpToRefresh();
            }
        });
    }

    /**
     * 设置头尾样式
     */
    private void setPullToRefreshLable() {
        ILoadingLayout startLoading = mPullToListView.getLoadingLayoutProxy(
                true, false);
        startLoading.setPullLabel("下拉刷新");// 刚下拉时显示的提示
        startLoading.setRefreshingLabel("正在刷新中...");// 刷新时显示的提示
        startLoading.setReleaseLabel("释放即可刷新");// 下拉达到一定距离时显示的提示

        ILoadingLayout endLoading = mPullToListView.getLoadingLayoutProxy(false,
                true);
        endLoading.setPullLabel("上拉加载更多");// 刚上拉时显示的提示
        endLoading.setRefreshingLabel("拼命加载中...");// 加载时的提示
        endLoading.setReleaseLabel("释放即可加载更多");// 上拉达到一定距离时显示的提示
    }

    /**
     * 下拉刷新
     */
    private void pullDownToRefresh() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    mDataList.clear();
                    for (int i = 0; i < 10; i++) {
                        mDataList.add("下拉刷新后的数据" + i + "\t号");
                    }
                    // size = str.size();设置加载数据的条数
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(0);
            }
        }).start();
    }

    /**
     * 上拉加载
     */
    private void pullUpToRefresh() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    for (int i = 0; i < 5; i++) {
                        mDataList.add("上拉加载后的数据" + i + "\t号");
                    }
                    // size = str.size();设置加载数据的条数
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(0);
            }
        }).start();
    }
}
